package com.sunseaypw.springmvc.web.controller;

import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.ModelAndView;

@Controller("rootBean")
public class Test1 {
    
    @RequestMapping(value="index", method = RequestMethod.GET)
    public String index(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception{
        System.out.println("enter in Test1111111............");
            ServletContext sc=arg0.getServletContext();    

            java.util.Iterator<String> it =sc.getAttributeNames().asIterator();
            while(it.hasNext()){
                System.out.println(it.next());
            }
            
            Object rc =  sc.getAttribute("org.springframework.web.context.WebApplicationContext.ROOT");
            Object rb = ((WebApplicationContext)rc).getBean("rootBean");
            
            Object slc =  sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.hello666");
            System.out.println("--------------"+slc.getClass());
            
            //override 根context的配置！！！！
            Object rb2 = ((AnnotationConfigWebApplicationContext)slc).getBean("rootBean");
            System.out.println("--------------"+rb2.getClass());    
            Object rb2222 = ((AnnotationConfigWebApplicationContext)slc).getBean("rootBean2222");
            System.out.println("--------------"+rb2222.getClass());    

            //遍历servlet所有bean
            AnnotationConfigWebApplicationContext scontext =((AnnotationConfigWebApplicationContext)slc);
            System.out.println("Servlet Context下bean的个数："+scontext.getBeanDefinitionCount());
            for(String bname : scontext.getBeanDefinitionNames()){
                System.out.println("----------beanname:"+bname+"  class:"+scontext.getBean(bname).getClass());
            }
            //遍历根所有bean
            XmlWebApplicationContext rcontext =((XmlWebApplicationContext)rc);
            System.out.println("Root Context下bean的个数："+rcontext.getBeanDefinitionCount());
            for(String bname : rcontext.getBeanDefinitionNames()){
                System.out.println("----------beanname:"+bname+"  class:"+scontext.getBean(bname).getClass());
            }


            var b=1/0;

            ModelAndView mv = new ModelAndView();
            mv.setViewName("index");
            PrintWriter pw = arg1.getWriter();
            pw.write("hhhkkkkjkj");
            pw.flush();
            return null;
    }

}
